package xyz.xmh.template;

import xyz.xmh.cache.Cache;
import xyz.xmh.cache.NullCache;
import xyz.xmh.dao.file.FileDao;
import xyz.xmh.dao.recycle.RecycleDao;
import xyz.xmh.executor.BaseExecutor;
import xyz.xmh.executor.CacheExecutor;
import xyz.xmh.executor.Executor;
import xyz.xmh.pojo.entity.BaseItem;
import xyz.xmh.pojo.enums.CategoryEnum;
import xyz.xmh.pojo.enums.CheckNameEnum;
import xyz.xmh.pojo.request.file.ListRequest;
import xyz.xmh.pojo.request.file.MultiDownloadUrlRequest;
import xyz.xmh.pojo.request.file.SearchRequest;
import xyz.xmh.pojo.request.file.UpdateRequest;
import xyz.xmh.pojo.response.file.*;

import java.nio.file.OpenOption;
import java.nio.file.Path;

/**
 * 2021/10/26 15:04 星期二<br/>
 * 适用于获取请求响应的实体
 *
 * @author xuMingHai
 */
public class ClientTemplate implements Executor {

    /**
     * 客户端执行器
     */
    private final Executor executor;

    /**
     * 缓存实例
     */
    private final Cache cache;

    public ClientTemplate(Cache cache, FileDao fileDao, RecycleDao recycleDao) {
        final BaseExecutor baseExecutor = new BaseExecutor(fileDao, recycleDao);
        this.cache = cache;

        if (NullCache.NULL_CACHE.equals(cache.getName())) {
            this.executor = baseExecutor;
        } else {
            this.executor = new CacheExecutor(cache, baseExecutor);
        }
    }

    /**
     * 获取缓存实例
     *
     * @return 缓存实例，如果没有开启缓存会返回空对象缓存
     */
    public Cache getCache() {
        return cache;
    }

    @Override
    public ListResponse list() {
        return executor.list();
    }

    @Override
    public ListResponse list(ListRequest listRequest) {
        return executor.list(listRequest);
    }

    @Override
    public SearchResponse search(String fileName) {
        return executor.search(fileName);
    }

    @Override
    public SearchResponse search(String fileName, CategoryEnum categoryEnum) {
        return executor.search(fileName, categoryEnum);
    }

    @Override
    public SearchResponse search(SearchRequest searchRequest) {
        return executor.search(searchRequest);
    }

    @Override
    public BaseItem get(String fileId) {
        return executor.get(fileId);
    }

    @Override
    public DownloadUrlResponse getDownloadUrl(String fileId) {
        return executor.getDownloadUrl(fileId);
    }

    @Override
    public long downloadFile(String fileId, Path path, OpenOption... openOptions) {
        return executor.downloadFile(fileId, path, openOptions);
    }

    @Override
    public MultiDownloadUrlResponse getMultiDownloadUrl(MultiDownloadUrlRequest multiDownloadUrlRequest) {
        return executor.getMultiDownloadUrl(multiDownloadUrlRequest);
    }

    @Deprecated
    @Override
    public long multiDownloadFile(MultiDownloadUrlRequest multiDownloadUrlRequest, Path path, OpenOption... openOptions) {
        return executor.multiDownloadFile(multiDownloadUrlRequest, path, openOptions);
    }

    @Override
    public CreateFolderResponse createFolder(String parentFileId, String name, CheckNameEnum checkNameEnum) {
        return executor.createFolder(parentFileId, name, checkNameEnum);
    }

    @Override
    public CreateFolderResponse createFolder(String parentFileId, String name) {
        return executor.createFolder(parentFileId, name);
    }

    @Override
    public CreateFolderResponse createFolder(String name) {
        return executor.createFolder(name);
    }

    @Override
    public CreateFileResponse uploadFile(String parentFileId, Path path, CheckNameEnum checkNameEnum) {
        return executor.uploadFile(parentFileId, path, checkNameEnum);
    }

    @Override
    public CreateFileResponse uploadFile(String parentFileId, Path path) {
        return executor.uploadFile(parentFileId, path);
    }

    @Override
    public CreateFileResponse uploadFile(Path path) {
        return executor.uploadFile(path);
    }

    @Override
    public BaseItem update(UpdateRequest updateRequest) {
        return executor.update(updateRequest);
    }

    @Override
    public BaseItem update(String fileId, String name) {
        return executor.update(fileId, name);
    }

    @Override
    public boolean trash(String fileId) {
        return executor.trash(fileId);
    }
}
